/*********************************************************
          File Name:regex.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Thu 30 Jul 2015 10:18:49 AM CST
**********************************************************/

#include <stdio.h>
#include <string.h>
#include <regex.h>
#include <stdlib.h>


void match(char *str, char *pattern)
{
        regex_t re;
        int res, eflag = REG_NOTBOL;
        int matches = 1;
        regmatch_t pmatch[matches];
        char out[256] = {0};

        if(regcomp(&re, pattern, REG_EXTENDED) != 0)
        {
                perror("regcomp()");
                exit(1);
        }
        res = regexec(&re, str, matches, pmatch, eflag);

        regfree(&re);

        if(res == 0)
        {
                snprintf(out, pmatch[0].rm_eo - pmatch[0].rm_so + 1,
                                "%s", str + pmatch[0].rm_so);
                printf("%s\n", out);
        }
}

int main(void)
{
        char *pattern = " a.*/[a-z||0-9]+$";
        char *str = "this is a foo/ab1";
        match(str, pattern);
        return 0;
}
